package sxt.main;

import sxt.util.GameUtil;

import javax.swing.*;
import java.awt.*;

/**
 * 小人类
 */
public class GamePerson {
    //矩形
    public Rectangle rect;
    //小人图片
    Image pup;
    Image pdown;
    Image pleft;
    Image pright;
    //存储路径
    private static final String PERSON_IMAGE = "img/up.png";
    private static final String PUP_IMAGE = "img/down.png";
    private static final String PLEFT_IMAGE = "img/left.png";
    private static final String PRIG_IMAGE = "img/right.png";
    //初始化参数
    public GamePerson() {
        pup = GameUtil.loadBufferedImage(PERSON_IMAGE);
        pdown = GameUtil.loadBufferedImage(PUP_IMAGE);
        pleft = GameUtil.loadBufferedImage(PLEFT_IMAGE);
        pright = GameUtil.loadBufferedImage(PRIG_IMAGE);
        rect = new Rectangle();
    }
    //小人配置
    int x,y;
    //小人方向
    public boolean up=false,down=false,left=false,right=false;

    int b=0;
    boolean aa = true;
    /**
     * 绘制小人
     */
    public void draw(Graphics g,int x,int y) {
       if (b==0) {
           g.drawImage(pdown, x, y, null);
           rect(g);
       }
       else{
           direction(g);
           rect(g);
       }
       if(aa){
           this.x=x;
           this.y=y;
           aa=false;
       }
    }
    /**
     * 控制小人方向
     */
    public void walk(int num){
        switch (num){
            case 1:
                up=true;
                down=false;
                left=false;
                right=false;
                y-=50;
                b=1;
                break;
            case 2:
                down=true;
                up=false;
                left=false;
                right=false;
                y+=50;
                b=1;
                break;
            case 3:
                up=false;
                down=false;
                left=true;
                right=false;
                x-=50;
                b=1;
                break;
            case 4:
                right=true;
                up=false;
                down=false;
                left=false;
                x+=50;
                b=1;
                break;
        }
    }
    /**
     * 切换小人图片
     */
    public void direction(Graphics g){
        if (up){
            g.drawImage(pup,x,y,null);
        }else if (down){
            g.drawImage(pdown,x,y,null);
        }
        else if (left){
            g.drawImage(pleft,x,y,null);
        }
        else if (right){
            g.drawImage(pright,x,y,null);
        }
    }
    /**
     * 绘制小人矩形
     */
    public void rect(Graphics g){
        rect.x=this.x;
        rect.y=this.y;
        rect.width=50;
        rect.height=50;
        g.setColor(Color.red);
        g.drawRect(rect.x,rect.y,rect.width,rect.height);
    }
}
